package com.zw.yzk.kabu.view.widget;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zw.yzk.kabu.data.cache.db.Exhibition;

/**
 * Created by wei on 2016/1/25.
 */
public class CustomAlertDialog {

    //对话框属性设置
    public static class AlertParams {
        public Context context;
        //dialog视图布局
        public View view;
        //标题id
        public int titleId;
        //标题文字
        public String titleText;
        //内容id
        public int contentId;
        //内容文字
        public String contentText;
        //左按键id
        public int negativeButtonId;
        //左按键文字
        public String negativeButtonText;
        //左按键点击事件监听
        public View.OnClickListener negativeButtonListener;
        //右按键id
        public int positiveButtonId;
        //右按键文字
        public String positiveButtonText;
        //右按键点击事件监听
        public View.OnClickListener positiveButtonListener;
        //是否可点击区域外取消
        public boolean cancelable;
        //设置style
        public int animation = -1;

        public AlertParams(Context context, View view) {
            this.context = context;
            this.view = view;
            this.cancelable = true;
        }
    }


    public static class Builder {
        private final AlertParams P;

        public Builder(Context context, int layoutId) {
            this(context, LayoutInflater.from(context).inflate(layoutId, null));
        }

        public Builder(Context context, View view) {
            P = new AlertParams(context, view);
        }

        public Builder setTitle(int titleId, String title) {
            P.titleId = titleId;
            P.titleText = title;

            return this;
        }

        public Builder setContent(int contentId, String content) {
            P.contentId = contentId;
            P.contentText = content;

            return this;
        }

        public Builder setNegativeButton(int negativeButtonId, String negativeButtonText) {
            P.negativeButtonId = negativeButtonId;
            P.negativeButtonText = negativeButtonText;

            return this;
        }

        public Builder setPositiveButton(int positiveButtonId, String positiveButtonText) {
            P.positiveButtonId = positiveButtonId;
            P.positiveButtonText = positiveButtonText;

            return this;
        }

        public Builder setOnNegativeButtonClickedListener(View.OnClickListener listener) {
            P.negativeButtonListener = listener;

            return this;
        }

        public Builder setOnPositiveButtonClickerListener(View.OnClickListener listener) {
            P.positiveButtonListener = listener;

            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            P.cancelable = cancelable;

            return this;
        }

        public Builder setAnimation(int animation) {
            P.animation = animation;

            return this;
        }

        public AlertDialog build() {
            final AlertDialog dialog = new AlertDialog.Builder(P.context).setView(P.view).setCancelable(P.cancelable).create();
            //设置背景透明
            try {
                dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                if (P.animation != -1) {
                    dialog.getWindow().setWindowAnimations(P.animation);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            View negativeView = P.view.findViewById(P.negativeButtonId);
            if (negativeView != null) {
                negativeView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (P.negativeButtonListener != null) {
                            P.negativeButtonListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
            }
            View positiveView = P.view.findViewById(P.positiveButtonId);
            if (positiveView != null) {
                positiveView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (P.positiveButtonListener != null) {
                            P.positiveButtonListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
            }
            try {
                Button negative = (Button) P.view.findViewById(P.negativeButtonId);
                if (negative != null) {
                    negative.setText(P.negativeButtonText);
                }
            } catch (Exception e1) {
                TextView negative = (TextView) P.view.findViewById(P.negativeButtonId);
                if (negative != null) {
                    negative.setText(P.negativeButtonText);
                }
            }
            try {
                Button positive = (Button) P.view.findViewById(P.positiveButtonId);
                if (positive != null) {
                    positive.setText(P.positiveButtonText);
                }
            } catch (Exception e1) {
                TextView positive = (TextView) P.view.findViewById(P.positiveButtonId);
                if (positive != null) {
                    positive.setText(P.positiveButtonText);
                }
            }
            TextView title = (TextView) P.view.findViewById(P.titleId);
            if (title != null) {
                title.setVisibility(View.VISIBLE);
                title.setText(P.titleText);
            }
            TextView content = (TextView) P.view.findViewById(P.contentId);
            if (content != null) {
                content.setVisibility(View.VISIBLE);
                content.setText(P.contentText);
            }
            return dialog;
        }
    }
}
